﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Project;
using System.Data;

namespace WebApp.Settings
{
    public partial class Projects : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {              
                if (!IsPostBack)
                {                    
                    // добавляем кнопки удаления          
                    DataControlField DeleteButtons = new ButtonField();
                    (DeleteButtons as ButtonField).CommandName = "DeleteRow";
                    (DeleteButtons as ButtonField).ButtonType = ButtonType.Image;
                    (DeleteButtons as ButtonField).ImageUrl = "~/images/delete.png";
                    ProjectsGridView.Columns.Add(DeleteButtons);

                    // добавляем кнопки редактирования
                    DataControlField EditButtons = new ButtonField();
                    (EditButtons as ButtonField).CommandName = "EditRow";
                    (EditButtons as ButtonField).ButtonType = ButtonType.Image;
                    (EditButtons as ButtonField).ImageUrl = "~/images/edit.gif";
                    ProjectsGridView.Columns.Add(EditButtons);

                }

                // параметры таблицы
                ProjectsGridView.AllowSorting = true;

                // получаем список проектов
                ProjectsGridView.DataSource = BuildIProjects.Build().GetAllProjects();
                ProjectsGridView.DataBind();

            }
            catch (Exception ex)
            {
                
            }
        }

        protected void ProjectsGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                if (e.CommandName == "DeleteRow")
                {
                    int index = Convert.ToInt32(e.CommandArgument);

                    DataTable dt = ProjectsGridView.DataSource as DataTable;

                    string id_project = dt.Rows[index]["#"].ToString();

                    BuildIProjects.Build().DeleteProject(id_project);

                    dt.Rows.RemoveAt(index);

                    ProjectsGridView.DataSource = dt;
                    ProjectsGridView.DataBind();

                }
                else if (e.CommandName == "EditRow")
                {
                    int index = Convert.ToInt32(e.CommandArgument);

                    DataTable dt = ProjectsGridView.DataSource as DataTable;

                    string id_project = dt.Rows[index]["#"].ToString();

                    Response.AddHeader("id_project", id_project);
                    Response.AddHeader("action", "edit");
                    Response.Redirect("~/Settings/ProjectForm.aspx");
                }
            }

            catch (Exception ex)
            {

            }

        }

        protected void ProjectsGridView_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
            }
            catch (Exception ex)
            {

            }
        }      
    }
}